package com.example.test_event_and_callback.infras.async;

import java.lang.reflect.Method;
import java.util.Map;

public abstract class AsyncProviderProcessor<MethodsType> {

    private Class<?> demanderClass;
    protected Map<String, MethodsType> providerMethodMap;

    public Map<String, MethodsType> getProviderMethodMap() {
        return providerMethodMap;
    }

    public AsyncProviderProcessor(Class<?> demanderClass) {
        this.demanderClass = demanderClass;
    }

    public static AsyncProviderProcessor<?> getInstance(Class<?> demanderClass, MethodCallType methodCallType){
        switch (methodCallType){
            case JAVA_REFLECTION -> {
                return new ProviderProcessorReflection(demanderClass);
            }
            case METHOD_HANDLE -> {
                return new ProviderProcessorMethodHandle(demanderClass);
            }
            case DIRECT_CALL -> throw new RuntimeException("提供者暂不支持直接调用");
        }
        throw new RuntimeException("调用策略不存在");
    }

    abstract void loadProviderMethodMap(Map<String,Method> mappedMethodByParam);

    //        for (Method annotatedMethod : annotatedMethodSet) {
//            AsyncDataProvider providerAnnotation = annotatedMethod.getAnnotation(AsyncDataProvider.class);
//            if (providerAnnotation.demander().equals(demanderClass)){
//                String demanderParamName = providerAnnotation.value();
//                putMethodToMap(providerMethodMap, demanderParamName, annotatedMethod);
//            }
//        }


    abstract void putMethodToMap( Map<String, MethodsType> map, String paramName, Method method);

    abstract Demander proxyDataDemander(Object demander);

    //TODO 现在第一个参数每次调用的时候都要进行一次强制转换，具体详见各实现类，我不想这样
    abstract Object executeProviderMethod(Object method, Object provider, String s);


}
